package com.erps.sys.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.erps.CommonUtils.DateUtils;
import com.erps.CommonUtils.StringEx;
import com.erps.dao.ErspResPictureDao;
import com.erps.db.DBManager;
import com.hmkj.entity.ErspResPicture;
import com.hmkj.entity.ErspSysOrg;

public class ErspSysOrgRest {
	public int Count(int iStartDay, int iFinishDay) {
		String cStartDay = String.valueOf(iStartDay) + "000000";
		String cFinishDay = String.valueOf(iFinishDay) + "999999";
		return Count(cStartDay, cFinishDay);
	}

	public int Count(String cStartDay, String cFinishDay) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) from ersp_sys_org ");
		sql.append(" where create_time>'" + cStartDay + "'");
		sql.append(" and create_time<'" + cFinishDay + "'");
		return StringEx.getInt(DBManager.GetStrValue(sql.toString()));
	}

	public int CountLast() {
		Date vDate = new Date();
		String cStartDay;
		String cFinishDay;
		int iMonth = vDate.getMonth();
		if (iMonth > 6) {
			cStartDay = DateUtils.getDateStr(new Date(), "yyyy") + "0000000000";
			cFinishDay = DateUtils.getDateStr(new Date(), "yyyy") + "0800000000";
		} else {
			String cYear = DateUtils.getDateStr(new Date(), "yyyy");
			cFinishDay = cYear + "0000000000";
			int iYear = StringEx.getInt(cYear) - 1;
			cStartDay = String.valueOf(iYear) + "0800000000";
		}
		return Count(cStartDay, cFinishDay);
	}

	public List<ErspSysOrg> Query() {
		List<Map<String, String>> dtRows = QueryAsMap();
		int iLength = (dtRows == null) ? 0 : dtRows.size();
		List<ErspSysOrg> arKeys = new ArrayList<ErspSysOrg>();
		for (int i = 0; i < iLength; i++) {
			Map<String, String> rs = dtRows.get(i);
			ErspSysOrg vo = new ErspSysOrg();
			vo.setId(StringEx.getMapVal(rs, "id"));
			vo.setOrg_area(StringEx.getInt(StringEx.getMapVal(rs, "org_area")));
			vo.setOrg_name(StringEx.getMapVal(rs, "org_name"));
			vo.setOrg_addr(StringEx.getMapVal(rs, "org_addr"));
			try {
				vo.setOrg_posx(Double.parseDouble(StringEx.getMapVal(rs, "org_posx")));
				vo.setOrg_posy(Double.parseDouble(StringEx.getMapVal(rs, "org_posy")));
			} catch (Exception ex) {

			}
			vo.setSys_path(StringEx.getMapVal(rs, "sys_path"));
			arKeys.add(vo);
		}
		return arKeys;

	}

	public List<Map<String, String>> QueryAsMap() {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * ");
		sql.append(" from ersp_sys_org ");
		return DBManager.QueryData(sql.toString());
	}

	public ErspSysOrg FindOne(String cKeyID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * ");
		sql.append(" from ersp_sys_org ");
		sql.append(" where id='" + cKeyID + "'");
		ErspSysOrg vo = new ErspSysOrg();

		Map<String, String> rs = DBManager.GetRow(sql.toString());
		vo.setId(StringEx.getMapVal(rs, "id"));
		vo.setOrg_area(StringEx.getInt(StringEx.getMapVal(rs, "org_area")));
		vo.setOrg_name(StringEx.getMapVal(rs, "org_name"));
		vo.setOrg_addr(StringEx.getMapVal(rs, "org_addr"));
		try {
			vo.setOrg_posx(Double.parseDouble(StringEx.getMapVal(rs, "org_posx")));
			vo.setOrg_posy(Double.parseDouble(StringEx.getMapVal(rs, "org_posy")));
		} catch (Exception ex) {

		}
		vo.setSys_path(StringEx.getMapVal(rs, "sys_path"));
		return vo;

	}

	public PageData Query(int iPageNO, int iPageSize, String cWhereParm) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) ");
		sql.append(" from ersp_sys_org ");
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(" where  " + cWhereParm);
		}
		// 添加Where条件

		String cOrderBy = " order by last_update_time desc ";

		int iRowCount = StringEx.getInt(DBManager.GetStrValue(sql.toString()));
		int iPageCount = (int) Math.ceil(iRowCount / (double) iPageSize);
		if (iPageNO < 0)
			iPageNO = 1;
		else if (iPageNO > iPageCount)
			iPageNO = iPageCount;

		sql.setLength(0);
		sql.append(" select * ");
		sql.append(" from ersp_sys_org ");
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(" where  " + cWhereParm);
		}

		ErspSysOrgRest rest = new ErspSysOrgRest();
		List<Map<String, String>> dtRows = DBManager.QueryData(sql.toString(), cOrderBy, iPageNO, iPageSize);
		PageData ret = new PageData();
		ret.setRowcount(iRowCount);
		ret.setPagecount(iPageCount);
		ret.setPageno(iPageNO);
		ret.getKeylist().addAll(dtRows);

		return ret;
	}

}